package com.xuzimian.globaldemo.rpc.hessian;

import com.caucho.hessian.client.HessianProxyFactory;
import org.junit.jupiter.api.Test;

import java.util.List;

/**
 * 要远程调用hessian服务，需要先部署一个web服务，并在该web服务中发布hessian服务（servlet）
 * 本示例可以在tomcat（等servlet容器）中直接配置项目下/web/web-inf/web.xml文件作为web服务启动。
 * 配置如下：
 * ---------------------------------------
 *  <servlet>
 *         <servlet-name>book</servlet-name>
 *         <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
 *         <init-param>
 *             <param-name>home-api</param-name>
 *             <param-value>com.xuzimian.globaldemo.rpc.hessian.BookService</param-value>
 *         </init-param>
 *         <init-param>
 *             <param-name>home-class</param-name>
 *             <param-value>com.xuzimian.globaldemo.rpc.hessian.BookServiceImpl</param-value>
 *         </init-param>
 *     </servlet>
 *     <servlet-mapping>
 *         <servlet-name>book</servlet-name>
 *         <uri-pattern>/book</uri-pattern>
 *     </servlet-mapping>
 * ---------------------------------------------------------
 * @program: global-demo
 * @description: 远程调用hessian服务的客户端
 * @author: xzm
 * @create: 2019-01-10 10:53
 **/
public class HessianClient {
    @Test
    public void testHessian() {
        String url = "http://127.0.0.1:8080/book";
        HessianProxyFactory factory = new HessianProxyFactory();
        try {
            BookService bookService = (BookService) factory.create(BookService.class, url);
            List<Book> list = bookService.getList();
            for (Book book : list) {
                System.out.println(book.getName() + ",定价为：" + book.getPrice() + "元。");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}